<?php
namespace app\model;
use think\Model;

class Recruit extends Model
{
    public function getInfoByWorkNum($num){
        try {
            $data = $this->where('work_num',$num)->find();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
        }
    }

    public function getAllByRow($list_rows,bool $simple = false,$config,$post){
        $select_post = array_diff_key ( $post, ['list_rows'=>-1,'page'=>-1] );
        try {
            $data = $this->where($select_post)->paginate($list_rows,$simple,$config)->toArray();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
        }
    }

    public function getAll(){
        try {
            $data = $this->select()->toArray();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
        }
    }

    public function deleteAll(){
        try {
            $this->where(true)->delete(true);
            $a = $this->getLastSql();
            // 无条件删除所有数据
            return [true,$a];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
        }
    }

    public function getAllWorkNum(){
        try {
            $data = $this->withAttr('work_num')->select();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    }

    public function updateRoles($num,$roles){
        try {
            $roles = 6 - $roles;
            $data = $this->where('work_num', $num)->update(['roles' => $roles]);
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    }

    public function updateStatus($num,$status){
        try {
            // $status = 6 - $status;
            $flag = 0;
            if($status ==='已通过'){
                $flag = 1;
            }
            // return $status;
            $data = $this->where('id', $num)->update(['status' => $flag]);
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    }
}
